using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FBGraph.Models;
using FBGraph.Web.Mvc;

namespace FBGraph.Demos.AspNetMvc.Controllers
{
    public abstract class BaseController : FBGraphMvcController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (FbContext.IsAuthorized)
            {
                ViewData["Name"] = FbContext.Users.Get("me").Name;
            }
        }

        public ActionResult Delete(String id)
        {
            if (!CreatedIds.Contains(id)) throw new Exception("For safety purposes, this demo will not delete objects that were not created in the current session.");
            
            var result = FbContext.Delete(id);

            if (Request.UrlReferrer == null) return null;
            else return Redirect(Request.UrlReferrer.ToString());
        }

        public List<String> CreatedIds
        {
            get
            {
                if (Session["Created"] == null) Session["Created"] = new List<String>();
                return (List<String>)Session["Created"];
            }
        }
    }
}